package com.example.music_mediaplayer.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.example.music_mediaplayer.MainActivity;
import com.example.music_mediaplayer.model.MessageEvent;
import org.greenrobot.eventbus.EventBus;

public class MyReceiver extends BroadcastReceiver {
    private Message msg = new Message();
    @Override
    public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "change_img":
                    Log.i("opopop", "111111");
                    String change = intent.getStringExtra("play_or_pause");
                    if (msg == null){
                        msg = new Message();
                    }else {
                        msg.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putString("change",change);
                        msg.setData(bundle);
                        MainActivity.handler.sendMessage(msg);
                    }
                    break;
                case "seekBar":
                    Log.i("opopop", "222222");
                    if (msg == null){
                        msg = new Message();
                    }else {
                        msg.arg1 = intent.getIntExtra("duration",0);
                        msg.arg2 = intent.getIntExtra("currentTime",0);
                        msg.what = 2;
                        MainActivity.handler.sendMessage(msg);
                        EventBus.getDefault().post(new MessageEvent(msg.arg2));
                    }
                    break;
                case "change_name_artist_photo":
                    Log.i("opopop", "33333");
                    String songName = intent.getStringExtra("song_name");
                    String songArtist = intent.getStringExtra("song_artist");
                    String songPhoto = intent.getStringExtra("song_photo");
                    Bundle bundle = new Bundle();
                    bundle.putString("songName",songName);
                    bundle.putString("songArtist",songArtist);
                    bundle.putString("songPhoto",songPhoto);
                    msg.setData(bundle);
                    msg.what = 3;
                    MainActivity.handler.sendMessage(msg);
                    break;
            }
    }
}
